package com.erpoa.controller;

import com.alibaba.fastjson.JSON;
import com.erpoa.annotation.NoRepeatSubmit;
import com.erpoa.dto.DealMessage;
import com.erpoa.entity.PackMessage;
import com.erpoa.service.BatchInspectInkService;
import com.erpoa.utils.PDAException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @description:
 * @author: jamin.chen
 * @createDate: 2020/8/28 9:37
 * @version: 1.0
 */
@CrossOrigin(maxAge = 3600)
@Api(value = "BatchInspecInkController", tags = {"墨水批次巡检pda接口"})
@RestController
@RequestMapping("/PDAService")
public class BatchInspecInkController {
    private static final Logger logger = LoggerFactory.getLogger(BatchInspecInkController.class);

    @Autowired
    private BatchInspectInkService batchInspectInkService;

    /**
     * wuLiuFahuoServlet
     *
     * @return
     * @throws Exception
     */
    @ApiOperation("墨水批次巡检pda综合接口")
    @RequestMapping(value = {"/batchInspecInkServlet"}, method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @NoRepeatSubmit(key = "batchInspecInk:arg[0]")
    public DealMessage batchInspecInkServlet(@RequestBody PackMessage packMessage) {
        DealMessage dealMessage = new DealMessage(true);
        int optType = packMessage.getOptType();
        logger.debug("线程:" + Thread.currentThread().getId() + "请求开始：" + packMessage.toString());
        System.out.println("线程:" + Thread.currentThread().getId() + "请求开始：" + packMessage.toString());

        try {
            switch (optType) {
                //根据机台号带出批次信息
                case 0:
                    dealMessage.setMessage(JSON.toJSONString(batchInspectInkService.selectRecmatDetailByMachine(packMessage)));
                    break;
                //记录扫描信息
                case 1:
                    dealMessage.setMessage(batchInspectInkService.putDataLog(packMessage));
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof PDAException) {
                dealMessage = new DealMessage(false, e.getMessage());
            } else {
                dealMessage = new DealMessage(false, "服务器hj接口出现未知错误，请再扫一次确认结果，如多次出现请及时反馈IT，分机号：6257！时间点：" + System.currentTimeMillis() + "错误信息：" + e.getMessage());
            }
            logger.debug("线程:" + Thread.currentThread().getId() + "请求结束：" + dealMessage.toString());
            System.out.println("线程:" + Thread.currentThread().getId() + "请求结束：" + dealMessage.toString());
            return dealMessage;
        }
        logger.debug(dealMessage.toString());
        logger.debug("线程:" + Thread.currentThread().getId() + "请求结束：" + dealMessage.toString());
        System.out.println("线程:" + Thread.currentThread().getId() + "请求结束：" + dealMessage.toString());
        return dealMessage;
    }


}
